package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户服务接口实现类
 * @author wangxin
 * @version 1.0
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    /**
     * 根据用户名查询数据用户信息
     * @param username
     * @return
     */
    @Override
    public User findByUserName(String username) {
        return userDao.findByUserName(username);
    }

    /**
     * 分页查询所有用户
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //1.设置分页参数
        PageHelper.startPage(currentPage,pageSize);
        //2.需要分页的语句
        Page<User> userPage = userDao.selectByCondition(queryString);
        return new PageResult(userPage.getTotal(),userPage.getResult());
    }

    /**
     * 删除用户
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        //根据用户id查询用户与角色表的关系
        int count = userDao.findRoleIdByUserId(id);
        if (count > 0){
            throw new RuntimeException(MessageConstant.DELETE_ROLEANDUSER_FAIL);
        }else {
            //直接删除
            userDao.deleteById(id);
        }
    }
}
